#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash lpsolve-5.5.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] && echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] && echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar

wget -t 40 -c -P $buildpath https://udomain.dl.sourceforge.net/project/lpsolve/lpsolve/5.5.2.11/lp_solve_5.5.2.11_source.tar.gz --no-check-certificate
tar -xzvf $buildpath/lp_solve_5.5.2.11_source.tar.gz -C $buildpath && rm -rf $buildpath/lp_solve_5.5.2.11_source.tar.gz

cd $buildpath/lp_solve_5.5/lpsolve55
sed -i 's/c=cc/c=clang/' ccc
sh ccc

mv $buildpath/lp_solve_5.5 $installpath/lp_solve_5.5

cat >"$installpath/lp_solve_5.5/lpsolve_modulefiles" <<EOF
#%Module1.0
conflict lpsolve
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
        set modfile_path [file dirname \$modfile]
            return \$modfile_path
    }
set pwd [getModulefileDir]
set LPSOLVE \$pwd
setenv LPSOLVE \$LPSOLVE
prepend-path LD_LIBRARY_PATH \$LPSOLVE/lpsolve55/bin/ux64
EOF
